package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.lang.Validator;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.SignUpRecordEntity;
import io.renren.modules.lanling.service.SignUpRecordService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 一键报名记录表
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-11-26 11:45:09
 */
@RestController
@RequestMapping("lanling/signuprecord")
public class SignUpRecordController {
    @Autowired
    private SignUpRecordService signUpRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:signuprecord:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = signUpRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:signuprecord:info")
    public R info(@PathVariable("id") Integer id) {
        SignUpRecordEntity signUpRecord = signUpRecordService.selectById(id);

        return R.ok().put("signUpRecord", signUpRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:signuprecord:save")
    public R save(@RequestBody SignUpRecordEntity signUpRecord) {

        //todo 参数校验
        if (!Validator.isMobile(signUpRecord.getPhone())) {
            return R.error("请输入正确的手机号码");
        }
        if (!Validator.isCitizenId(signUpRecord.getIdcard())) {
            return R.error("请输入正确的身份证号码");
        }


        signUpRecord.setCreateDate(new Date());
        signUpRecordService.insert(signUpRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:signuprecord:update")
    public R update(@RequestBody SignUpRecordEntity signUpRecord) {
        ValidatorUtils.validateEntity(signUpRecord);

        //todo 参数校验
        if (!Validator.isMobile(signUpRecord.getPhone())) {
            return R.error("请输入正确的手机号码");
        }
        if (!Validator.isCitizenId(signUpRecord.getIdcard())) {
            return R.error("请输入正确的身份证号码");
        }

        //全部更新
        signUpRecordService.updateAllColumnById(signUpRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:signuprecord:delete")
    public R delete(@RequestBody Integer[] ids) {
        signUpRecordService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
